# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


class MaoyanPipeline(object):
    def process_item(self, item, spider):
        print(item['name'], item['stars'], item['atime'])
        return item


import pymysql


class MaoYanMySQLPipeline(object):
    def open_spider(self, spider):
        """打开数据库"""
        self.db = pymysql.connect(
            host='localhost',
            user='root',
            password='123456',
            database='maoyan_db',
            charset='utf8'
        )
        self.cursor = self.db.cursor()

    def process_item(self, item, spider):
        """将item的数据，用SQL语句插入到数据库"""
        insert_sql = 'insert into filmtab values(%s, %s, %s)'
        data = (item['name'], item['stars'], item['atime'])
        self.cursor.execute(insert_sql, data)
        self.db.commit()
        return item

    def close_spider(self, spider):
        """此方法会在爬虫结束时调用,在此关闭数据库"""
        self.cursor.close()
        self.db.close()

